package com.lifeng.finance.demo.controller;

import com.lifeng.finance.demo.entity.Customer;
import com.lifeng.finance.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@ComponentScan(basePackages = {"com.lifeng.finance.demo.service.*"})
@RestController
public class CustomerController {


    @Autowired
    private CustomerService customerService;


    @RequestMapping(path = "/customer/all")
    public List<Customer> finAll() {
        return customerService.findAll();
    }

    @RequestMapping(path = "/customer/by_id")
    public Customer findOneById(@RequestParam(name = "id") int id) {
        return customerService.findOneById(id);
    }

    @RequestMapping(path = "/customer/by_name")
    public List<Customer> findSomeByName(@RequestParam(name = "name") String name) {
        return customerService.findSomeByName(name);
    }

    @RequestMapping(path = "/customer/add")
    public Customer add(@RequestParam(name = "name") String name) {
        Customer customer = new Customer(name);
        customerService.add(customer);
        return customer;
    }

    @RequestMapping(path = "/customer/update")
    public Customer update(@RequestParam(name = "id") int id, @RequestParam(name = "name") String name) {

        Customer customer = customerService.findOneById(id);
        customer.setName(name);
        customerService.update(customer);
        return customer;
    }

}
